﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Namespace Buddies
    Friend Class DBController
        Shared Sub New()
            Dim aCols() As String = {"Id", "Name", "NameLocal", "Host", "Port" _
                                   , "LastConnected", "Comment" _
                                   , "PairUpPinCode", "PairUpValidUntil" _
                                   , "PassCodeMine/PassCode_LoginHere", "PassCodeBuddy/PassCode_LoginAtBuddy" _
                                   , "UniqueId", "Email"}
            ObjectPersister.DefineObject(GetType(Entities.Buddy), "Buddies", aCols)
        End Sub

        Private Sub New()
        End Sub

        Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As Framework.Data.TransactionScope)
            Dim strSql As String = "DELETE FROM Buddies WHERE Id=@Id"
            Dim sqlPar1 As New SqlCeParameter("@Id", id)
            Dim iRowsAffected As Integer = Framework.Data.Environment.ExecuteNonQuery(strSql, transactionScope, sqlPar1)
        End Sub

        Public Shared Sub Save(ByVal buddy As Entities.Buddy, ByVal transactionScope As Framework.Data.TransactionScope)
            Dim clsCmd As New SaveCommand(Of Integer)("Buddies", "Id", buddy.Id, 0)
            clsCmd.AddParam("Name", buddy.Name, True)
            clsCmd.AddParam("NameLocal", buddy.NameLocal, True)
            clsCmd.AddParam("Host", buddy.Host, True)
            clsCmd.AddParam("Port", buddy.Port, True)
            clsCmd.AddParam("LastConnected", buddy.LastConnected, True)
            clsCmd.AddParam("Comment", buddy.Comment, True)
            clsCmd.AddParam("PairUpPinCode", buddy.PairUpPinCode, True)
            clsCmd.AddParam("PairUpValidUntil", buddy.PairUpValidUntil, True)
            clsCmd.AddParam("PassCodeMine", buddy.PassCode_LoginHere, True)
            clsCmd.AddParam("PassCodeBuddy", buddy.PassCode_LoginAtBuddy, True)
            clsCmd.AddParam("Email", buddy.Email, True)
            clsCmd.AddParam("UniqueId", buddy.UniqueId, True)

            buddy.Id = clsCmd.Save(transactionScope)
        End Sub

        Public Shared Function GetAll() As Generic.List(Of Entities.Buddy)
            Return ObjectPersister.GetAll(Of Entities.Buddy)()
        End Function

        Public Shared Function GetById(ByVal id As Integer) As Entities.Buddy
            Return ObjectPersister.GetOne(Of Entities.Buddy)(String.Format("Id={0}", id))
        End Function
        Public Shared Function GetByUniqueId(ByVal uniqueId As String) As Entities.Buddy
            Return ObjectPersister.GetOne(Of Entities.Buddy)(String.Format("UniqueId='{0}'", uniqueId))
        End Function

    End Class
End Namespace